package ${groupId}.${artifactId}.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jradweb.core.controller.JMultiActionController;
import org.springframework.web.servlet.ModelAndView;
import org.jradweb.core.utils.BeanUtils;
 
import ${groupId}.${artifactId}.service.${service.name}Service; 
import ${groupId}.${artifactId}.model.${service.name};

/**
 * ${controller}Controller class .
 * 
 * Created on ${createdTime}
 * @author Code Generator
 * @version $Id$
 */
public class ${controller}Controller extends JMultiActionController{
	private static final Log log = LogFactory.getLog(${controller}Controller.class);
	
	 
	private ${service.name}Service ${service.lowerCaseFirst}Service;
	 
	
	 
	public void set${service.name}Service(${service.name}Service ${service.lowerCaseFirst}Service){
		this.${service.lowerCaseFirst}Service= ${service.lowerCaseFirst}Service;
	}
	public ${service.name}Service get${service.name}Service(){
		return this.${service.lowerCaseFirst}Service;
	}
	

	public Map<String, Object> index(HttpServletRequest request,
			HttpServletResponse response) throws Exception {		
		Map<String,Object> map=new HashMap<String,Object>();
		String strPage=request.getParameter("page");
		Integer page=strPage==null?1:Integer.parseInt(strPage);
		 
		List<${model}> ${service.lowerCaseFirst}List=this.${service.lowerCaseFirst}Service.getPage(page,this.getPageSize());
		Integer totalPage=this.${service.lowerCaseFirst}Service.getTotalPageCount(this.getPageSize());
		map.put("${service.lowerCaseFirst}List", ${service.lowerCaseFirst}List);
		map.put("totalPage", totalPage);
		map.put("page", page);
		return map;
	}
	
	public Map<String, Object> show(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = (String) request.getParameter("id");

		${model} ${model.lowerCaseFirst} = this.get${service}Service().get${model}(Integer.parseInt(id));
		map.put("${model.lowerCaseFirst}",  ${model.lowerCaseFirst} );

		return map;

	}
	
	public ModelAndView save(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
	
		${model} ${model.lowerCaseFirst} = new ${model}();
		BeanUtils.setProperties(${model.lowerCaseFirst}, request);

		if (${model.lowerCaseFirst}.hasErrors(request.getLocale())) {
			map.put("${model.lowerCaseFirst}", ${model.lowerCaseFirst});
			return this.render("create", map);
		}
		this.${service.lowerCaseFirst}Service.save(${model.lowerCaseFirst});

		this.getFlash(request).put("message",
				"message.saved.success");
		map.put("id", ${service.lowerCaseFirst}.getId());

		return this.redirect("show", map);
	}
	
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = (String) request.getParameter("id");

		${model} ${model.lowerCaseFirst} = this.get${model}Service().get${model}(Integer.parseInt(id));
	
		this.${model.lowerCaseFirst}Service.delete(${model.lowerCaseFirst});
		this.getFlash(request).put("message", "message.deleted.success");
		
		return this.redirect("index", map);
	}
	
	public Map<String,Object> create(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		return map;
	}

	public Map<String, Object> edit(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = (String) request.getParameter("id");

		${model} ${model.lowerCaseFirst} = this.get${model}Service().get${model}(Integer.parseInt(id));
		map.put("${model.lowerCaseFirst}", ${model.lowerCaseFirst});
		return map;
	}
	
	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = (String) request.getParameter("id");

		${model} ${model.lowerCaseFirst} = this.get${model}Service().get${model}(Integer.parseInt(id));
		BeanUtils.setProperties(${model.lowerCaseFirst}, request);

		if (${model.lowerCaseFirst}.hasErrors(request.getLocale())) {
			map.put("${model.lowerCaseFirst}", ${model.lowerCaseFirst});
			return this.render("edit", map);
		}
		this.${model.lowerCaseFirst}Service.update(${model.lowerCaseFirst});

		this.getFlash(request).put("message",
				"message.updated.succuess");
		map.put("id", id);

		return this.redirect("show", map);
	}
}